using Avalonia.BiliBili.Libs.Models.View;
using Avalonia.BiliBili.Units;
using Avalonia.BiliBili.ViewModels;
using Avalonia.Controls;
using Avalonia.Extensions.Event;
using Bilibili.Common;
using Bilibili.Common.Log;
using Bilibili.Model.Bangumi;
using Bilibili.WebClient.Api;
using DynamicData;
using System;
using System.Diagnostics;

namespace Avalonia.BiliBili.Windows
{
    public partial class TimelineWindow : WindowBase
    {
        private BangmiClient Client { get; }
        private TimelineViewModel ViewModel { get; }
        public TimelineWindow()
        {
            InitializeComponent();
            Client = new BangmiClient();
            ViewModel = new TimelineViewModel();
        }
        protected override void AfterInitializeComponent()
        {
            this.DataContext = ViewModel;
        }
        public void OnItemClick(object sender, ViewRoutedEventArgs e)
        {
            if (e.ClickItem is ListBoxItem item && item.Content is TimeLineItem timeLine)
                Navigater.OpenWindow<BanInfoWindow>(timeLine.Season_Id);
        }
        public override void Callback()
        {
            try
            {
                if (Data is not int type)
                    type = 0;
                RunOnUiThread(async () =>
                {
                    var result = await Client.GetTimeline(type);
                    if (result.IsSuccess())
                    {
                        foreach (var item in result.Data)
                        {
                            var header = $"{item.Date}";
                            var tabItem = new TabControlItem<TimeLineItem>(header);
                            tabItem.Nodes.AddRange(item.Seasons);
                            ViewModel.Items.Add(tabItem);
                        }
                    }
                });
            }
            catch (Exception ex)
            {
                LogManager.Instance.LogError("Callback", ex);
            }
        }
    }
}